﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ITMAN.AssetMan_FRMs
{
    public partial class FRM_mdt_details : Form
    {
        public FRM_mdt_details()
        {
            InitializeComponent();
        }

        public static string asset_nr;

        private void FRM_mdt_details_Load(object sender, EventArgs e)
        {
            Klassen.CL_assetman assetman = new Klassen.CL_assetman();
            DGV_mdt_details.DataSource = assetman.get_mdt_detail_to_datatable(asset_nr);
        }

        private void BTN_bearbeiten_Click(object sender, EventArgs e)
        {
            if (DGV_mdt_details.ReadOnly)
            {
                DGV_mdt_details.ReadOnly = false;
                if (DGV_mdt_details.RowCount < 1)
                {
                    DGV_mdt_details.AllowUserToAddRows = true;
                }
                BTN_bearbeiten.BackColor = Color.Red;
            }
            else
            {
                DGV_mdt_details.ReadOnly = true;
                DGV_mdt_details.AllowUserToAddRows = false;
                BTN_bearbeiten.BackColor = Color.LightGray;
            }
        }

        private void DGV_mdt_details_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
            DGV_mdt_details.AllowUserToAddRows = false;
        }

        private void BTN_save_Click(object sender, EventArgs e)
        {
            Klassen.CL_assetman assetman = new Klassen.CL_assetman();
            DataTable tbl = assetman.get_mdt_detail_to_datatable(asset_nr);
            if (tbl.Rows.Count > 0)
            {
                if (assetman.update_mdt_details(asset_nr, DGV_mdt_details.Rows[0]))
                {
                    MessageBox.Show("Update erfolgreich");
                    this.Close();
                }
            }
            else
            {
                assetman.insert_new_mdt(DGV_mdt_details.Rows[0], asset_nr);
                this.Close();
            }
        }

        private void DGV_mdt_details_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                DGV_mdt_details.Rows[0].Cells[0].Value = asset_nr;
                DGV_mdt_details.Rows[0].Cells[0].ReadOnly = true;
            }
        }
    }
}
